package com.kmzx.one.project.config.listener;

import com.kmzx.one.common.constants.UpMode;
import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.UserPay;
import com.kmzx.one.project.entity.UserRank;
import com.kmzx.one.project.entity.UserRankBuy;
import com.kmzx.one.project.entity.UserRankDetail;
import com.kmzx.one.project.events.SelfToUpEvent;
import com.kmzx.one.project.events.UserRankBuyEvent;
import com.kmzx.one.project.mapper.SimpleMapper;
import com.kmzx.one.project.mapper.UserPayMapper;
import com.kmzx.one.project.mapper.UserRankMapper;
import com.kmzx.one.project.others.dso.UserRankDso;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("userRankBuyEventListener")
public class UserRankBuyEventListener {


    private static Logger logger = LoggerFactory.getLogger(UserRankBuyEventListener.class);

    @Autowired
    private SimpleMapper simpleMapper;

    @Autowired
    private UserRankMapper userRankMapper;

    @Autowired
    private UserPayMapper userPayMapper;

    @Autowired
    ApplicationContext applicationContext;



    @EventListener(classes = {UserRankBuyEvent.class})
    @Transactional(rollbackFor = Exception.class)
    @Async
    public void defaultUserRankBuyEvent(UserRankBuyEvent userRankBuyEvent) {
        logger.info("用户购买等级,回调事件,{}",userRankBuyEvent);

        UserRankBuy rankBuy = userRankBuyEvent.getUserRankBuy();

        String userId = rankBuy.getUserId();

        Date date = rankBuy.getPayTime();

        List<UserPay> userPays = simpleMapper.selectUserPayByUserId(userId);

        UserRank userRank = userRankMapper.selectUserRankByUserId(userId);

        UserRankDetail detail = new UserRankDetail();
        detail.setId(UUIDGenerator.generate());
        detail.setRemark("购买货仓");
        detail.setUserId(rankBuy.getUserId());
        detail.setFromUserId(rankBuy.getUserId());
        detail.setGrowthValue(rankBuy.getPayMoney().doubleValue());
        detail.setCreateTime(date);
        detail.setUpMode(UpMode.buy);
        detail.setOrdersId(rankBuy.getOrdersCode());

        userRankMapper.insertUserRankDetail(detail);

        BigDecimal payMoney = rankBuy.getPayMoney();

        if(userRank!=null){
           /* List<UserRankDso> tabs = userRankMapper.selectAllRankTabs();
            Double myGrowthValue = userRank.getMyGrowthValue();
            BigDecimal add = payMoney.add(BigDecimal.valueOf(myGrowthValue));

            String rankId = userRank.getRankId();
            userRank.setMyGrowthValue(add.doubleValue());*/
           /* for (int i = 0; i < tabs.size(); i++) {
                if (tabs.get(i).getRankId().equals(rankId) && i != tabs.size() - 1) {
                    BigDecimal rankPay = tabs.get(i).getRankPay();
                    if (rankPay.compareTo(add) <= 0) {
                        userRank.setRankId(tabs.get(i + 1).getRankId());
                    }
                }
            }*/

            userRankMapper.updateUserRankBuyByIdSelective(rankBuy);
            logger.info("微信支付,购买等级,更新数据库");
            if (!CollectionUtils.isEmpty(userPays)) {
                UserPay userPay = userPays.get(0);
                BigDecimal old = userPay.getMoney();
                userPay.setMoney(payMoney.add(old));
                userPay.setUpdateTime(date);
                userPayMapper.updateByIdSelective(userPay);
            } else {
                UserPay userPay = new UserPay();
                userPay.setId(UUIDGenerator.generate());
                userPay.setCreateTime(date);
                userPay.setUpdateTime(date);
                userPay.setMoney(payMoney);
                userPay.setDeleted(0);
                userPay.setStates(0);
                userPay.setUserId(rankBuy.getUserId());
                userPayMapper.insert(userPay);
            }
            SelfToUpEvent toUpEvent = new SelfToUpEvent();
            toUpEvent.setSelfUserId(userRank.getUserId());
            toUpEvent.setSelfRankId(userRank.getRankId());
            toUpEvent.setSelfRankMoney(BigDecimal.valueOf(userRank.getMyGrowthValue()));
            toUpEvent.setBuyMoney(payMoney);
            toUpEvent.setOrderCodes(rankBuy.getOrdersCode());
            toUpEvent.setUserRank(userRank);
            applicationContext.publishEvent(toUpEvent);
         //   userRankMapper.updateByIdSelective(userRank);
        }

    }
}
